﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using CodeStream.Testing.WebAutomation.SpecFlow;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SpecFlowIntegrationTutorial
{
    [Binding]
    class SamplePageSteps
    {
        [Given("the page is loaded")]
        public void LoadThePage()
        {
            Page.NavigateToUrl(new Uri(BrowserSpecFlowTools.ServerUri, "Calculator.html"));
        }

        [Given("I have entered (.*) and (.*) into the page")]
        public void EnterOperands(string value1, string value2)
        {
            Page.First.Text = value1;
            Page.Second.Text = value2;
        }

        [When("I press calculate")]
        public void PressCalculate()
        {
            Mouse.Click(Page.Calculate);
        }

        [Then("the result on the page should be (.*)")]
        public void VerifyResult(int expected)
        {
            int actual;
            Assert.IsTrue(int.TryParse(Page.Result.InnerText, out actual), "The result is not an integer");
            Assert.AreEqual(expected, actual);
        }

        [Then("the result on the page should be invalid")]
        public void VerifyInvalidResult()
        {
            Assert.AreEqual("NaN", Page.Result.InnerText.Trim());
            System.Threading.Thread.Sleep(5000);
        }

        public static SampleDefinition.UINamePage Page
        {
            get
            {
                return BrowserSpecFlowTools.GetUIMap<SampleDefinition.UINamePage>("SamplePage", process => new SampleDefinition.UINamePage(process));
            }
        }

    }
}
